package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.domain.MarketCart;
import com.cskaoyan.market.service.wx.WxCartService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.wx.lhh.cartVo.CartAddVo;
import com.cskaoyan.market.vo.wx.lhh.cartVo.CartCheckedVo;
import com.cskaoyan.market.vo.wx.lhh.cartVo.CartUpdateVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WxCartController
 * @Description
 * @Author MZ
 * @Date 2023/5/21 17:44
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    WxCartService wxCartService;

    /**
     * @MethodName index
     * @Description 购物车首页
     * @return: java.lang.Object
     * @Author MZ
     * @Date 19:21 2023/5/21
     **/
    @GetMapping("index")
    public Object index() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            // 未登录
            return ResponseUtil.unlogin();
        }
        Map<String, Object> result = wxCartService.index(userId);

        return ResponseUtil.ok(result);
    }

    @PostMapping("add")
    public Object add(@RequestBody CartAddVo addVo) {
        // {"goodsId":1152161,"number":2,"productId":239}
        Integer goodsId = addVo.getGoodsId();
        Short number = addVo.getNumber();
        Integer productId = addVo.getProductId();

        // 返回添加完成后购物车的id号
        Integer cartId = null;
        try {
            cartId = wxCartService.addCart(productId, goodsId, number);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok(cartId);
    }

    @PostMapping("fastadd")
    public Object fastadd(@RequestBody CartAddVo addVo) {
        Integer goodsId = addVo.getGoodsId();
        Short number = addVo.getNumber();
        Integer productId = addVo.getProductId();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        MarketCart marketCart = wxCartService.addfast(productId, goodsId, number, userId);

        if (marketCart == null) {
            // 第一次添加购物车
            return add(addVo);
        }

        return ResponseUtil.ok(marketCart.getId());
    }

    @GetMapping("goodscount")
    public Object goodsCount() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            // 未登录
            return ResponseUtil.ok(0);
        }
        Long count = wxCartService.getGoodsCount(userId);

        return ResponseUtil.ok(count);
    }

    @PostMapping("checked")
    public Object checked(@RequestBody CartCheckedVo checkedVo) {

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        Integer[] productIds = checkedVo.getProductIds();
        Boolean isChecked = true;
        if (checkedVo.getIsChecked() == 0) {
            isChecked = false;
        }
        wxCartService.checked(productIds, isChecked, userId);

        return index();
    }

    @GetMapping("checkout")
    public Object checkout(Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId) {
        // cartId=0&addressId=16&couponId=0&userCouponId=-1&grouponRulesId=0
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        Map<String, Object> result = wxCartService.checkoutCart(cartId, addressId, couponId, userCouponId, grouponRulesId, userId);

        return ResponseUtil.ok(result);
    }

    //
    @PostMapping("update")
    public Object update(@RequestBody CartUpdateVo updateVo){
        // {"productId":103,"goodsId":1086015,"number":1,"id":377}
        Integer productId = updateVo.getProductId();
        Integer goodsId = updateVo.getGoodsId();
        Short number = updateVo.getNumber();
        Integer cartId = updateVo.getId();

        int row = wxCartService.updateCart(productId, goodsId, cartId,number);

        if (row != 1){
            return ResponseUtil.badArgumentValue();
        }
        return ResponseUtil.ok();
    }

    @PostMapping("delete")
    public Object delete(@RequestBody Map<String,Integer[]> map){
        // productIds":[234]
        Integer[] productIds = map.get("productIds");
        List<Integer> productIdsList = new ArrayList<>();
        for (Integer productId : productIds) {
            productIdsList.add(productId);
        }
        wxCartService.deleteCart(productIdsList);

        return index();
    }


}
